package cn.jbooter.shiro.autoconfigure.properties;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;

import cn.jbooter.shiro.autoconfigure.cache.CacheMode;


/**
 * 安全配置(基于shiro实现)
 * @author HeJian
 *
 */
@ConfigurationProperties(prefix = "spring.shiro")
public class IShiroProperties {

	/**
	 * 是否启用shiro自动配置
	 */
	private boolean enabled=false;
	
	/**
	 * 未经身份认证的用户跳转的登录地址
	 */
	private String loginUrl="/login.jsp";
	
	/**
	 * 登录成功后重定向的默认地址
	 */
	private String successUrl="/";
	
	/**
	 * 授权失败时重定向到的地址
	 */
	private String unauthorizedUrl=null;
	
	/**
	 * shiro web.xml中过滤器映射拦截路径,默认/*
	 */
	private String urlPattern="/*";
	
	/**
	 * Shiro过滤器链定义;(格式是:url=拦截器[参数],拦截器[参数])
	 */
	private List<String> filterChainDefinitions = new ArrayList<String>();
	
	/**
	 * shiro rememberme 配置
	 */
	private RememberMeManager rememberMeManager = new RememberMeManager();
	
	private CacheManager cacheManager = new CacheManager();
	
	
	public boolean isEnabled() {
		return enabled;
	}
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	public List<String> getFilterChainDefinitions() {
		return filterChainDefinitions;
	}
	public void setFilterChainDefinitions(List<String> filterChainDefinitions) {
		this.filterChainDefinitions = filterChainDefinitions;
	}
	public String getLoginUrl() {
		return loginUrl;
	}
	public void setLoginUrl(String loginUrl) {
		this.loginUrl = loginUrl;
	}
	public String getSuccessUrl() {
		return successUrl;
	}
	public void setSuccessUrl(String successUrl) {
		this.successUrl = successUrl;
	}
	public String getUnauthorizedUrl() {
		return unauthorizedUrl;
	}
	public void setUnauthorizedUrl(String unauthorizedUrl) {
		this.unauthorizedUrl = unauthorizedUrl;
	}
	public String getUrlPattern() {
		return urlPattern;
	}
	public void setUrlPattern(String urlPattern) {
		this.urlPattern = urlPattern;
	}
	public RememberMeManager getRememberMeManager() {
		return rememberMeManager;
	}
	public void setRememberMeManager(RememberMeManager rememberMeManager) {
		this.rememberMeManager = rememberMeManager;
	}
	public CacheManager getCacheManager() {
		return cacheManager;
	}
	public void setCacheManager(CacheManager cacheManager) {
		this.cacheManager = cacheManager;
	}


	/**
	 * shiro rememberme 配置
	 * @author HeJian
	 *
	 */
	public static class RememberMeManager{
		/**
		 * 是否启用rememberme功能,默认false
		 */
		private boolean enabled = false;
		/**
		 * remember me cookie 配置
		 */
		private Cookie cookie = new Cookie();
		/**
		 * 加密rememberMe Cookie的密钥,base64字符串,默认使用AES算法
		 */
		private String base64CipherKey = "NHVtI0hDI0l4NWdjMSNAVw==";
		
		public boolean isEnabled() {
			return enabled;
		}
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		public Cookie getCookie() {
			return cookie;
		}
		public void setCookie(Cookie cookie) {
			this.cookie = cookie;
		}
		public String getBase64CipherKey() {
			return base64CipherKey;
		}
		public void setBase64CipherKey(String base64CipherKey) {
			this.base64CipherKey = base64CipherKey;
		}


		/**
		 * rememberme cookie设置
		 * @author HeJian
		 *
		 */
		public static class Cookie{
			/**
			 * rememberMe cookie name, default : rememberMe
			 */
			private String name = "rememberMe";
			/**
			 * 过期时间,单位:毫秒, -1表示关闭浏览器失效此cookie,默认: -1
			 */
			private int maxAge=-1;
			/**
			 * true, 防止XSS攻击和窃取cookie增加cookie的安全性; 默认: true
			 */
			private boolean httpOnly=true;
			/**
			 * RememberMe cookie安全标识, 默认:false
			 */
			private boolean secure = false;
			/**
			 * 设置Cookie的域名,默认空,即当前访问的域名
			 */
			private String domain;
			/**
			 * 设置Cookie的路径,默认空,即存储在域名根下
			 */
			private String path;
			public String getName() {
				return name;
			}
			public void setName(String name) {
				this.name = name;
			}
			public int getMaxAge() {
				return maxAge;
			}
			public void setMaxAge(int maxAge) {
				this.maxAge = maxAge;
			}
			public boolean isHttpOnly() {
				return httpOnly;
			}
			public void setHttpOnly(boolean httpOnly) {
				this.httpOnly = httpOnly;
			}
			public boolean isSecure() {
				return secure;
			}
			public void setSecure(boolean secure) {
				this.secure = secure;
			}
			public String getDomain() {
				return domain;
			}
			public void setDomain(String domain) {
				this.domain = domain;
			}
			public String getPath() {
				return path;
			}
			public void setPath(String path) {
				this.path = path;
			}
		}
	}
	
	/**
	 * 缓存管理器
	 * @author HeJian
	 *
	 */
	public static class CacheManager{
		/**
		 * 是否启用缓存,默认为true,启用.
		 */
		private boolean enabled = true;
		/**
		 * 缓存管理器模式,使用shiro提供的缓存管理器还是委托给spring的缓存管理器
		 */
		private CacheMode cacheMode = CacheMode.SHIRO;
		
		/**
		 * 使用shiro提供的ehcache缓存管理器的ehcache配置文件,默认使用classpath:shiro/ehcache/shiro-ehcache.xml配置文件.
		 * 当且仅当缓存管理器模式为shiro才生效
		 */
		private String shiroCacheManagerConfigFile="classpath:shiro/ehcache/shiro-ehcache.xml";
		

		public boolean isEnabled() {
			return enabled;
		}
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		public CacheMode getCacheMode() {
			return cacheMode;
		}
		public void setCacheMode(CacheMode cacheMode) {
			this.cacheMode = cacheMode;
		}
		public String getShiroCacheManagerConfigFile() {
			return shiroCacheManagerConfigFile;
		}
		public void setShiroCacheManagerConfigFile(String shiroCacheManagerConfigFile) {
			this.shiroCacheManagerConfigFile = shiroCacheManagerConfigFile;
		}
	}
	
}
